package edu.zut.cs.network.bank.user.dao.impl;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.mysql.jdbc.PreparedStatement;

import edu.zut.cs.network.bank.user.dao.UserDao;
import edu.zut.cs.network.bank.user.entity.User;
//import jdbc.demo.JdbcGetConnection;

public class UserDaoImpl implements UserDao{
	
	private static Logger logger = Logger.getLogger(UserDaoImpl.class);
	private  String url;
	private  String user ;
	private  String password ;
	private  String driver;
	private  Connection connection = null;
	private  ResultSet rSet = null;
	private  PreparedStatement pst = null;

	
	public UserDaoImpl() {
		try {
			this.init("mysql.ini");
			Class.forName(driver);
			connection = DriverManager.getConnection(url, user, password);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

	}
	

	public void init(String fileName)
	{
		try {
			Properties props = new Properties();
			props.load(new FileInputStream(fileName));
			driver = props.getProperty("driver");
			url = props.getProperty("url");
			user = props.getProperty("user");
			password = props.getProperty("pass");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public int addUser(User user) {
		try {
		String s = "insert into user (name,fee) values('"+user.getName()+"','"+user.getFee()+"')";	
		logger.info(s);
		logger.info("\n");
		pst = (PreparedStatement) connection.prepareStatement(s);//创建 PrepareStatement对象
		pst.executeUpdate();//运行更新操作，返回更新的行数。			
		}catch(SQLException e) {
		e.printStackTrace();
	}finally {
		try {
			//			
		pst.close();
		connection.close();
		}catch (SQLException e) {
			e.printStackTrace();
		}
	}
		return 0;
	}

	public int updateUser(User user) {
		try {
		String s = "update user set name=?,fee=? where id= "+ user.getId();
		logger.info(s);
		logger.info("\n");
		pst = (PreparedStatement) connection.prepareStatement(s);//创建 PrepareStatement对象
		pst.setString(1, user.getName());
		pst.setDouble(2, user.getFee());
		pst.executeUpdate();//运行更新操作，返回更新的行数。		
	}catch(SQLException e) {
		e.printStackTrace();
	}finally {
		try {
			//关闭资源			
		pst.close();
		connection.close();
		}catch (SQLException e) {
			e.printStackTrace();
		}
	}
		return 0;
	}

	public int deleteUser(Long id) {
		try{
			String s = "delete from user where id = " + id;
			logger.info(s);
			logger.info("\n");
			pst = (PreparedStatement) connection.prepareStatement(s);//创建 PrepareStatement对象
			pst.executeUpdate();//运行更新操作，返回更新的行数。		
			}catch(SQLException e) {
				e.printStackTrace();
			}finally {
				try {
					//关闭资源			
				pst.close();
				connection.close();
				}catch (SQLException e) {
					e.printStackTrace();
				}
			}
		return 0;
	}

	public User getUserById(Long id) {
		try {
			String s = "select * from user where id =" +id;		
			logger.info(s);
			logger.info("\n");
			pst = (PreparedStatement) connection.prepareStatement(s);//创建 PrepareStatement对象
			//处理数据库返回的结果（使用ResultSet类）
			rSet = pst.executeQuery();
			
			while(rSet.next()) {
				User user = new User();
				user.setId(rSet.getLong("id"));
				user.setName(rSet.getString("name"));
				user.setFee(rSet.getDouble("fee"));
				logger.info("Found User:"+ user +"By Id\n");
			}	
	
		}catch(SQLException e) {
			e.printStackTrace();
		}finally {
			try {				
			rSet.close();
			pst.close();
			connection.close();
			}catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public static void main(String[] args)
	{
		UserDaoImpl udi = new UserDaoImpl();
		User user = new User();
		user.setName("zhangtenghui");
		user.setFee(1000);
		udi.addUser(user);
	}
}